﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SDM.ExamPlanner.DALC;
using SDM.ExamPlanner.DTO;
using SDM.ExamPlanner.BLL;

namespace SDM.ExamPlanner.Test
{
    [TestClass]
    public class ExamTest : BaseTest
    {
        [TestMethod]
        public void AddExamTest()
        {
            // Assign
            var examRepository = new DataRepository<Exam>();

            // Act
            examRepository.RegisterNew(new Exam 
            { 
                Course =TestObjects.ListCourseObject(),
                Teacher=TestObjects.ListTeacherObject(),
                Classroom=TestObjects.ClassRoomObject1(),
                Education=TestObjects.EducationObject1(),
                SchoolClass=TestObjects.SchoolClassObject1()
            });
            
            examRepository.SaveChanges();
            var exams = examRepository.Find();

            // Assert
            Assert.AreEqual(1, exams.Count());
        }

        [TestMethod]
        public void GetListOfExamsTest()
        {
            // Assign
            var examRepository = new DataRepository<Exam>();

            // Act
            examRepository.RegisterNew(new Exam
            {
                Course = TestObjects.ListCourseObject(),
                Teacher = TestObjects.ListTeacherObject(),
                Classroom = TestObjects.ClassRoomObject1(),
                Education = TestObjects.EducationObject1(),
                SchoolClass = TestObjects.SchoolClassObject1()
            });

            examRepository.RegisterNew(new Exam
            {
                Course = TestObjects.ListCourseObject(),
                Teacher = TestObjects.ListTeacherObject(),
                Classroom = TestObjects.ClassRoomObject2(),
                Education = TestObjects.EducationObject2(),
                SchoolClass = TestObjects.SchoolClassObject2()
            });

            examRepository.SaveChanges();
            var exams = examRepository.Find();

            // Assert
            Assert.AreEqual(2, exams.Count());
        }

        [TestMethod]
        public void EditExamTest()
        {
            // Assign
            var educationRepository = new DataRepository<Education>();
            var education = new Education { Name = "Computer Science" };

            // Act
            educationRepository.RegisterNew(education);
            educationRepository.SaveChanges();

            // Assert
            Assert.AreNotEqual(0, education.Id);
        }

        [TestMethod]
        public void ShowAllExamsForSemester()
        {
            // Assign
            var examRepository = new DataRepository<Exam>();
            int semester;
            var schoolClass = TestObjects.SchoolClassObject1();
            var sortedExams = new List<Exam>();

            // Act
            semester = new Manager<SchoolClass>().CalculateSemester(schoolClass);

            examRepository.RegisterNew(new Exam
            {
                Course = TestObjects.ListCourseObject(),
                Teacher = TestObjects.ListTeacherObject(),
                Classroom = TestObjects.ClassRoomObject1(),
                Education = TestObjects.EducationObject1(),
                SchoolClass = schoolClass
            });

            examRepository.RegisterNew(new Exam
            {
                Course = TestObjects.ListCourseObject(),
                Teacher = TestObjects.ListTeacherObject(),
                Classroom = TestObjects.ClassRoomObject2(),
                Education = TestObjects.EducationObject2(),
                SchoolClass = TestObjects.SchoolClassObject2()
            });

            examRepository.SaveChanges();
            var exams = examRepository.Find();

            foreach (var exam in exams)
            {
                if (new Manager<SchoolClass>().CalculateSemester(exam.SchoolClass) == semester)
                    sortedExams.Add(exam);
            }

            // Assert
            Assert.AreEqual(1, sortedExams.Count());
        }

        [TestMethod]
        public void DeleteExamTest()
        {
            // Assign
            var examRepository = new DataRepository<Exam>();
            var exam = new Exam
            {
                Course = TestObjects.ListCourseObject(),
                Teacher = TestObjects.ListTeacherObject(),
                Classroom = TestObjects.ClassRoomObject1(),
                Education = TestObjects.EducationObject1(),
                SchoolClass = TestObjects.SchoolClassObject1()
            };

            // Act
            examRepository.RegisterNew(exam);
            examRepository.SaveChanges();
            examRepository.RegisterDeleted(exam);
            examRepository.SaveChanges();

            // Assert
            Assert.IsFalse(examRepository.Find().Any());
        }

        [TestMethod]
        public void GetListOfExamsByEducationTest()
        {
            // Assign
            var examRepository = new DataRepository<Exam>();

            // Act
            examRepository.RegisterNew(new Exam
            {
                Course = TestObjects.ListCourseObject(),
                Teacher = TestObjects.ListTeacherObject(),
                Classroom = TestObjects.ClassRoomObject1(),
                Education = TestObjects.EducationObject1(),
                SchoolClass = TestObjects.SchoolClassObject1()
            });

            examRepository.RegisterNew(new Exam
            {
                Course = TestObjects.ListCourseObject(),
                Teacher = TestObjects.ListTeacherObject(),
                Classroom = TestObjects.ClassRoomObject2(),
                Education = TestObjects.EducationObject2(),
                SchoolClass = TestObjects.SchoolClassObject2()
            });

            examRepository.SaveChanges();
            var exams = examRepository.Find().Where(e => e.Education.Name == "Datamatiker");

            // Assert
            Assert.AreEqual(1, exams.Count());
        }
    }
}
